package com.longrise.android.byjk.widget.recyclerviewadapter;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.longrise.common.utils.PrintLog;

/**
 * Created by sujizhong on 2017/3/2.
 * 判断是否滑动到RecyclerView的底部更加可靠
 */

public class RecyclerOnScrollListener2 extends RecyclerView.OnScrollListener {

    private int visibleThreshold = 300;

    public static final int STATUS_VISIABLE = 0;
    public static  final int STATUS_INVISIABLE = 1;

    private boolean mLoading;

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);

        if (recyclerView != null) {
            LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
            // fix RecyclerView can not scroll in SwipeRefreshLayout
            onScrollStatus(linearLayoutManager.findFirstCompletelyVisibleItemPosition() == 0);

            int computeVerticalScrollExtent = recyclerView.computeVerticalScrollExtent();
            int computeVerticalScrollOffset = recyclerView.computeVerticalScrollOffset();
            int computeVerticalScrollRange = recyclerView.computeVerticalScrollRange();

            if(!mLoading){
                if (computeVerticalScrollExtent + computeVerticalScrollOffset >= computeVerticalScrollRange) {
                    mLoading = true;
                    onLoadListener();
                }
            }
            onScrollX(computeVerticalScrollExtent + computeVerticalScrollOffset < computeVerticalScrollRange ? STATUS_INVISIABLE : STATUS_VISIABLE);
        }
    }

    public void onLoadListener() {
        PrintLog.e("TAG", "onLoadListener");

    }

    public void onScrollStatus(boolean enable) {

    }

    public void onScrollX(int status){

    }

    public void reset(){
        mLoading = false;
    }

}
